<template>

    <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="仓库名称">
            <el-input v-model="formInline.StorageName" clearable />
        </el-form-item>

        <el-form-item label="仓库编码">
            <el-input v-model="formInline.StorageCode" clearable />
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="StorageShow">查询</el-button>
            <el-button type="success" @click="storaAdd">添加</el-button>
        </el-form-item>
    </el-form>


    <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="序号" width="60" />

        <el-table-column prop="storageName" label="名称" width="100" />
        <el-table-column prop="storageCode" label="编码" width="90" />
        <el-table-column prop="storgeMsg" label="描述" width="100" />
        <el-table-column prop="shelfName" label="所属货架/库区" width="120" />
        <el-table-column prop="addProp" label="添加人" width="120" />
        <el-table-column prop="addTime" label="添加时间" width="190">
            <template #default="script">
                {{ moment(script.row.addTime).format("YYYY-MM-DD HH:mm:ss") }}
            </template>
        </el-table-column>
        <el-table-column prop="updTime" label="修改时间" width="200" />

        <el-table-column fixed="right" label="操作" min-width="120">
            <template #default>
                <el-button link type="primary" size="small" @click="handleClick">
                    Detail
                </el-button>
                <el-button link type="primary" size="small">Edit</el-button>
            </template>
        </el-table-column>
    </el-table>
    <el-pagination v-model:current-page="Page.PageIndex" v-model:page-size="Page.PageSize" :page-sizes="[1, 3, 5, 7]"
        layout="total, sizes, prev, pager, next, jumper" :total="Page.TotalCount" prev-text="上一页" next-text="下一页"
        @size-change="handleSizeChange" @current-change="handleCurrentChange" />

    <!-- 添加 -->
    <el-dialog v-model="storaAddVis" title="添加储位" width="500">
        <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto">

            <el-form-item label="储位编号" prop="storageCode">
                <el-input v-model="ruleForm.storageCode" />
            </el-form-item>

            <el-form-item label="储位名称" prop="storageName">
                <el-input v-model="ruleForm.storageName" />
            </el-form-item>

            <el-form-item label="储位描述" prop="storgeMsg">
                <el-input v-model="ruleForm.storgeMsg" />
            </el-form-item>

            <el-form-item label="所属货架" prop="shelfId">
                <el-select v-model="ruleForm.shelfId" placeholder="请选择">

                    <el-option v-for="item in sherlform" :key="item.id"
                        :label="item.shelfCode + '/' + item.shelfName + '/' + item.shelfMsg" :value="item.id" />

                </el-select>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)">
                    添加
                </el-button>
                <el-button @click="resetForm(ruleFormRef)">重置</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>

</template>

<script lang="ts" setup>
import { ref, onMounted, reactive } from 'vue'
import Mesaxios from '../../Http/MesHelp'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage, ElMessageBox } from 'element-plus'
import moment from 'moment'

const handleClick = () => {
    console.log('click')
}

const User = reactive({
    Name: localStorage.getItem("userNickname") || ''
})
// 显示 查询 分页
const tableData = ref([])
const formInline = reactive({
    StorageCode: "",
    StorageName: ""
})
const Page = reactive({
    PageIndex: 1,
    PageSize: 3,
    TotalCount: 0,
    TotalPage: 0
})

const StorageShow = () => {
    Mesaxios.get("/api/Storehouse/StorageShow", {
        params: {
            StorageCode: formInline.StorageCode, StorageName: formInline.StorageName,
            PageIndex: Page.PageIndex, PageSize: Page.PageSize
        }
    }).then(res => {
        tableData.value = res.data.data.list
        Page.TotalCount = res.data.data.count
        Page.TotalPage = res.data.data.Page
        console.log(tableData.value);
    })
}

const handleSizeChange = (val: number) => {
    console.log(`${val} items per page`)
    Page.PageSize = val
    StorageShow()
}
const handleCurrentChange = (val: number) => {
    console.log(`current page: ${val}`)
    Page.PageIndex = val
    StorageShow()
}


// 货架/库区信息列表显示方法
interface ShelfItem {
    id: number
    shelfCode: string
    shelfName: string
    shelfMsg: string
}

const sherlform = ref<ShelfItem[]>([])
const StorageGet = () => {
    Mesaxios.get("/api/Storehouse/StorageGet").then(res => {
        sherlform.value = res.data.data
        console.log(sherlform.value)
    })
}

//添加
const storaAdd = () => {
    //alert("sdasds");
    storaAddVis.value = true
}
const storaAddVis = ref(false)

interface RuleForm {
    storageCode: string
    storageName: string
    shelfId: any
    storgeMsg: string
    addProp: any
}

const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
    storageCode: "",
    storageName: "",
    shelfId: "",
    storgeMsg: "",
    addProp: User.Name
})

const locationOptions = ['Home', 'Company', 'School']

const rules = reactive<FormRules<RuleForm>>({
    storageCode: [
        { required: true, message: '储位编码不能为空', trigger: 'blur' },
    ],
    storageName: [
        { required: true, message: '储位名称不能为空', trigger: 'change' },
    ],
    shelfId: [
        { required: true, message: '请选择货架', trigger: 'change' },
    ],
    storgeMsg: [
        { required: true, message: '储位描述不能为空', trigger: 'change' },
    ],

})

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            console.log('submit!')

            console.log(ruleForm)
            Mesaxios.post("/api/Storehouse/StorageAdd", ruleForm).then(res => {
                if (res.data.code == 200) {
                    ElMessage.success("添加成功");
                    storaAddVis.value = false
                    StorageShow()
                }
                else {
                    ElMessage.error("添加失败");
                    storaAddVis.value = false
                    StorageShow()
                }
            })

        } else {
            console.log('error submit!', fields)
        }
    })
}

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
    value: `${idx + 1}`,
    label: `${idx + 1}`,
}))

onMounted(() => {
    StorageShow()
    StorageGet()
})
</script>